package com.gss.entity.generator;

import org.springframework.util.CollectionUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
 * 读取txt文件中的数据，解析转换为json
 */
public class ParseDataToJson {

    public void test() throws IOException {
        List<Map<String, String>> dataList = parseTxtData();
        printData(dataList);
    }


    /**
     * 打印生成的数据
     * @param dataList
     */
    private void printData(List<Map<String, String>> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        int count = 0;
        for (Map<String, String> map : dataList) {
            count++;
            //字段名称
            String fieldNameKey = "cell_" + count + "_1";
            String fieldName = map.get(fieldNameKey);
            //字段
            String fieldKey = "cell_" + count + "_2";
            String field = map.get(fieldKey);
            //字段首字母转换小写
            String str = field.charAt(0) + "";
            String newField = str.toLowerCase() + field.substring(1);
            //数据类型
            String typeKey = "cell_" + count + "_3";
            String type = map.get(typeKey);
            type = type.toLowerCase();
            String printType = "String";
            switch (type) {
                case "long":
                    printType = "Long";
                    break;
                case "int":
                    printType = "Integer";
                    break;
                case "double":
                    printType = "Double";
                    break;
                case "char":
                    printType = "char";
                    break;
                case "boolean":
                    printType = "Boolean";
                    break;
                default:
                    break;
            }
            //打印
            System.out.println();
            System.out.println("@ApiModelProperty(\""+ fieldName +"\")");
            System.out.println("@JSONField(name = \""+ field +"\")");
            System.out.println("private "+ printType +" "+ newField +";");
        }
    }

    /**
     * 读取txt文件数据，转换为json
     * @return
     * @throws IOException
     */
    public static List<Map<String,String>> parseTxtData() throws IOException {
        List<Map<String,String>> dataList = new ArrayList<>();

        String path = "C:\\Users\\DYang\\Desktop\\test.txt";

        File file = new File(path);
        if (!file.exists()) {
            System.out.println("文件不存在");
            return dataList;
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file)));
        String line = null;
        int count = 1;
        int cell = 0;
        boolean start = false;
        Map<String,String> map = null;
        while ((line = reader.readLine()) != null) {
            if (line.trim().equals(String.valueOf(count))) {
                start = true;
                count++;
                cell = 0;
                map = new TreeMap<>();
                dataList.add(map);
            }
            if (!start) {
                continue;
            }
            String key = "cell_" + (count - 1) + "_" + (cell++);
            map.put(key,line);
        }
        return dataList;
    }
}
